#include "GameScene.h"
#include "BlockBox.h"

USING_NS_CC;

Scene* GameScene::createScene()
{
	return GameScene::create();
}

bool GameScene::init()
{
	if (!Scene::init())
	{
		return false;
	}

   //xiaoming edit
	

	auto visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks.plist");


	auto spr = Sprite::create("bg2.png");
	this->addChild(spr);

	spr->setAnchorPoint(Vec2(0.5f, 1));
	spr->setPositionX(visibleSize.width/2 + origin.x);
	spr->setPositionY(visibleSize.height + origin.y);

	m_blockBoxLayer = Layer::create();
	m_blockBoxLayer->setPosition(origin);
	this->addChild(m_blockBoxLayer);

	m_map = new BlockBox**[m_mapSizeH];
	for (int i = 0; i < m_mapSizeH; i++)
	{
		m_map[i] = new BlockBox*[m_mapSizeW];
	}

	for (size_t i = 0; i < m_mapSizeH; i++)
	{
		for (size_t j = 0; j < m_mapSizeW; j++)
		{
			auto bb = BlockBox::create();
			bb->createBlock();
			bb->setDelegage(this);
			m_blockBoxLayer->addChild(bb);
			bb->setPositionX(i*bb->getContentSize().width);
			bb->setPositionY(j*bb->getContentSize().height);

			MapPosStruct MPS{i,j};

			bb->setMapPos(MPS);

			m_map[i][j] = bb;
		}

	}
	return true;
}

void GameScene::BlockClick(Node * node)
{
	auto bbox = static_cast<BlockBox*>(node);
	log("%d %d \n", bbox->getMapPos().x, bbox->getMapPos().y);
}